/*
 * Copyright (c) 2011, Shane Conder and Lauren Darcey
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are 
 * permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this list of 
 *   conditions and the following disclaimer.
 *   
 * * Redistributions in binary form must reproduce the above copyright notice, this list 
 *   of conditions and the following disclaimer in the documentation and/or other 
 *   materials provided with the distribution.
 *   
 * * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used
 *   to endorse or promote products derived from this software without specific prior 
 *   written permission.
 *   
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * <ORGANIZATION> = Mamlambo
 */
package com.mamlambo.easy3d;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.os.Bundle;

import com.mamlambo.easy3d.helpers.EasyCube;

public class Easy3DActivity extends Activity {
    private GLSurfaceView easySurfaceView = null;
    private Easy3DRenderer mEasyRenderer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        easySurfaceView = new GLSurfaceView(this);
        easySurfaceView.setEGLContextClientVersion(1);
        mEasyRenderer = new Easy3DRenderer(this);
        easySurfaceView.setRenderer(mEasyRenderer);

        setContentView(easySurfaceView);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (easySurfaceView != null) {
            easySurfaceView.onPause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (easySurfaceView != null) {
            easySurfaceView.onResume();
        }
    }

    private class Easy3DRenderer implements GLSurfaceView.Renderer {
        private Context context;
        private EasyCube.Textured texHoneyCube;
        private EasyCube.Textured texDroidCube;

        private float mRotateAngle = 0f;

        Easy3DRenderer(Context context) {
            this.context = context;
            texHoneyCube = new EasyCube.Textured(4.0f);
            texDroidCube = new EasyCube.Textured(1.0f);
        }

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
            gl.glShadeModel(GL10.GL_SMOOTH);

            gl.glDisable(GL10.GL_DITHER);
            gl.glEnable(GL10.GL_DEPTH_TEST);
            gl.glEnable(GL10.GL_CULL_FACE);
            gl.glEnable(GL10.GL_TEXTURE_2D);
            gl.glEnable(GL10.GL_COLOR_MATERIAL);
            gl.glEnable(GL10.GL_LIGHTING);
            gl.glEnable(GL10.GL_LIGHT0);
            gl.glEnable(GL10.GL_LIGHT1);

            gl.glActiveTexture(GL10.GL_TEXTURE0);

            gl.glClearColor(0.4f, 0.4f, 0.5f, 1);

            // generate the texture space
            int[] textures = new int[2];
            gl.glGenTextures(2, textures, 0);

            // assigned the textures

            texHoneyCube.setTexture(gl, context, textures[0],
                    R.drawable.honeycomb_512);
            texDroidCube.setTexture(gl, context, textures[1],
                    R.drawable.gingerbread_256);
        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            gl.glViewport(0, 0, width, height);

            float ratio = (float) width / height;
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();

            GLU.gluPerspective(gl, 35, ratio, 1, 500);
            // gl.glFrustumf(-ratio, ratio, -1, 1, 1, 50);
            GLU.gluLookAt(gl, 0, 0, 12f, 0, 0, 0, 0, 1, 0f);

            gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, new float[] { 0.2f,
                    0.2f, 0.2f, 1f }, 0);

            gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, new float[] { 1f, 1f,
                    1f, 1f }, 0);
            gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, new float[] { 1f, 1f,
                    1f, 1f }, 0);

            gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, new float[] { -20f,
                    20f, 0f, 1 }, 0);
            gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, new float[] { 5f,
                    -5f, 7f, 1 }, 0);
        }

        @Override
        public void onDrawFrame(GL10 gl) {

            gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

            gl.glMatrixMode(GL10.GL_MODELVIEW);
            gl.glLoadIdentity();

            gl.glTranslatef(0f, 0f, 0f);
            gl.glRotatef(mRotateAngle, 1f, 1f, 0f);
            texHoneyCube.draw(gl);

            gl.glLoadIdentity();

            gl.glScalef(5f, 5f, 5f);
            gl.glTranslatef(-5f, 2f, -10f);
            gl.glRotatef(mRotateAngle, 0f, 1f, 1f);
            // gl.glColor4f(1f, 0f, 0f, 1f);
            texDroidCube.draw(gl);

            gl.glLoadIdentity();

            gl.glTranslatef(3f, 0f, 0f);
            gl.glRotatef(mRotateAngle, 1f, 0f, 1f);
            // gl.glColor4f(1f, 0f, 0f, 1f);
            gl.glScalef(2.5f, 2.5f, 2.5f);
            texDroidCube.drawAgain(gl);

            mRotateAngle += 0.5f;
        }

    }
}